// pages/registered/registered.js
function countdown(that) {
  var second = that.data.countdown_time
  if (second == 0) {
    console.log("001");
    // console.log("Time Out...");  
    that.setData({
      // second: "Time Out..."  

      showtime1: true,
      showtime2: false,
      reg_hqyzm: "重新获取",
      countdown_time: 60,
    });
    console.log("002");
    return;
  }
  console.log("003");
  var time = setTimeout(function () {
    that.setData({
      countdown_time: second - 1
    });
    countdown(that);
  }
    , 1000)
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showtime1: true,
    showtime2: false,
    reg_hqyzm: "获取验证码",
    countdown_time: 60,
    value: '',
    phone:'',
    code:'',
    yzm:'',
    stu:'',
    items: [
      { name: 'USA', value: '男' },
      { name: 'CHN', value: '女', checked: 'true' },
    ],
    items1: [
      { name: 'USA', value: '有' },
      { name: 'CHN', value: '无', checked: 'true' },
    ],
  },
  mobileInputEvent: function (e) {
    this.setData({
      phone:e.detail.value
    })
  },
  codeInputEvent: function (e) {
    this.setData({
      code: e.detail.value
    })
    var code=this.data.code;
    var yzm=this.data.yzm;
    if(code==yzm){
      this.setData({
        stu:1
      })
    }else{
      wx.showToast({
        title: '验证码错误！',
        icon: 'none',
        duration: 1500
      })
    }
  },
  reg_yanzhengma_huoqu: function (e) {
    this.setData({
      showtime1: false,
      showtime2: true,
    });
    var phone = this.data.phone;
    var that=this;
    wx.request({
      url: "https://www.135k.com/addons/sd_humans/tp/index.php/register/send",
      data: {
        phone:phone
      },
      method: "post",
      success: function (res) {
        var yzm=res.data;
        that.setData({
          yzm:yzm
        })
      }
    })
    countdown(this);
    // wxTimer.start(this);  
  },
  submit: function (e) {
    var that = this;
    var code = this.data.code;
    var checked = e.detail.value.checkboxs[0];
    var radio1 = e.detail.value.radio1;
    var radio2 = e.detail.value.radio2;
    var phone = e.detail.value.phone;
    var password1 = e.detail.value.password1;
    var password2 = e.detail.value.password2;
    var name = e.detail.value.name;
    var code = e.detail.value.code;
    var school = e.detail.value.school;
    var consultant = e.detail.value.consultant; 
    var stu=that.stu
    //var checked = that.data.checked;    
    if (phone == "" || password1 == "" || password2 == "" || name == "" || code == "" || school == "" || consultant == "" || stu==1) {
      wx.showToast({
        title: '不能为空',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(phone))) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    else if (password1 !== password2) {
      wx.showToast({
        title: '密码不一致',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    else if (checked==undefined) {
      wx.showToast({
        title: '请先阅读兼职协议',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    else {
      wx.request({
        url: 'https://www.135k.com/addons/sd_humans/tp/index.php/register/restart_reset',
        data:{
          checked: checked,
          radio2: radio2,
          radio1: radio1,
          phone: phone,
          password: password1,
          name: name,
          code: code,
          school: school,
          code:code,
          consultant: consultant
        },
        method:"post",
        success:function(res){
          var stu=res.data.stu
          if (stu == 0){
            wx.showToast({
              title: res.data.msg,
              icon: 'fail',
              duration: 1500
            }); 
          }
          if(stu==1){
            wx.showToast({
              title: '注册成功',
              icon: 'success',
              duration: 1500            
            });  
              setTimeout(function () {//确认之后跳转的页面
                wx.navigateTo({
                  url: '../login/login'
                })
              }, 1500);
          }
        }
      }) 
    }
  },
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
  },
  checkboxChange: function (e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)

  },
  start_password: function () {
    wx.navigateTo({
      url: '../password/password',
    })
  },
  start_registered: function () {
    wx.navigateTo({
      url: '../registered/registered',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})